using System.Collections;

namespace CSharpPro2._1023;

public class Demo
{
    public void TestPrint()
    {
        int[] arr1 = new int[5];
        arr1[0] = 1;
        arr1[4] = 5;
        //Console.WriteLine(arr1[6]);//System.IndexOutOfRangeException

        for (int i = 0; i < arr1.Length; i++)
        {
            Console.Write(arr1[i] + "\t ");
        }
        String[] arr2 = new string[5];
        arr2[0] = "Hello";
        arr2[4] = "World";
        for (int i = 0; i < arr2.Length; i++)
        {
            Console.Write(arr2[i] + "\t ");
        }
        int[] arr3 = {1,2,3,4,5,6};
        Console.WriteLine(arr3[4]);
        
        int[,] arr4 = new int[3,3];
        arr4[0, 0] = 1;
        arr4[1, 1] = 2;
        arr4[2, 2] = 3;//右下角
        int[,] arr5 =
        {
            { 1, 2, 3 },
            { 4, 5, 6 },
            { 6, 7, 8 }

        };
        int[] arr6= { 12, 55, 27, 91, 66 };
        Array.Sort(arr6);
        Array.Reverse(arr6);//倒序和sort一起用有效  
        foreach (int i in arr6)
        {
            Console.Write(i + "\t");
        }


    }
     // 作业1：冒泡排序
        public void BubbleSort(int[] arr)
        {
            Console.WriteLine("=== 作业1：冒泡排序 ===");
            Console.WriteLine("排序前的数组：");
            PrintArray(arr);
            
            int n = arr.Length;
            for (int i = 0; i < n - 1; i++)
            {
                for (int j = 0; j < n - i - 1; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        // 交换元素
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
                
                Console.WriteLine($"第 {i + 1} 轮排序结果：");
                PrintArray(arr);
            }
            
            Console.WriteLine("排序后的数组：");
            PrintArray(arr);
            Console.WriteLine();
        }
        
        private void PrintArray(int[] arr)
        {
            foreach (int num in arr)
            {
                Console.Write(num + " ");
            }
            Console.WriteLine();
        }
        
        // 作业2：学生类演示
        public void StudentDemo()
        {
            Console.WriteLine("=== 作业2：学生类演示 ===");
            
            // 创建学生对象
            Student student1 = new Student("张三", 20, "计算机科学");
            Student student2 = new Student("李四", 19, "软件工程");
            Student student3 = new Student("王五", 21, "人工智能");
            
            // 显示学生信息
            student1.DisplayInfo();
            student2.DisplayInfo();
            student3.DisplayInfo();
            
            // 演示方法调用
            student1.Study();
            student2.TakeExam("C#程序设计");
            student3.UpdateGrade(95.5);
            
            Console.WriteLine();
        }
    }
    
    // 学生类
    public class Student
    {
        // 字段
        private string name;
        private int age;
        private string major;
        private double grade;
        
        // 属性
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        
        public int Age
        {
            get { return age; }
            set 
            { 
                if (value > 0 && value < 150)
                    age = value;
                else
                    Console.WriteLine("年龄不合法！");
            }
        }
        
        public string Major
        {
            get { return major; }
            set { major = value; }
        }
        
        public double Grade
        {
            get { return grade; }
            set 
            { 
                if (value >= 0 && value <= 100)
                    grade = value;
                else
                    Console.WriteLine("成绩必须在0-100之间！");
            }
        }
        
        // 构造函数
        public Student(string name, int age, string major)
        {
            this.name = name;
            this.age = age;
            this.major = major;
            this.grade = 0;
        }
        
        public Student(string name, int age, string major, double grade) : this(name, age, major)
        {
            this.grade = grade;
        }
        
        // 方法
        public void DisplayInfo()
        {
            Console.WriteLine($"学生信息：姓名={name}, 年龄={age}, 专业={major}, 成绩={grade}");
        }
        
        public void Study()
        {
            Console.WriteLine($"{name} 正在学习...");
        }
        
        public void TakeExam(string courseName)
        {
            Console.WriteLine($"{name} 正在参加 {courseName} 考试");
        }
        
        public void UpdateGrade(double newGrade)
        {
            this.grade = newGrade;
            Console.WriteLine($"{name} 的成绩已更新为：{newGrade}");
        }
        
        public bool IsExcellent()
        {
            return grade >= 90;
        }
    }
